package com.edu.koTA07.utils;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.edu.koTA07.bean.PesananDetailBean;
import com.edu.koTA07.sqlHelper.SQLHelper;

public class PesananDetailUtils {
	private static final String TAG = "cek";
	SQLHelper helper;
	SQLiteDatabase database;	
		
	public PesananDetailUtils(Context ctx) {
		helper = new SQLHelper(ctx);
		database = helper.getWritableDatabase();
	}
	
	@SuppressWarnings("static-access")
	public ArrayList<PesananDetailBean> loadData() {
		ArrayList<PesananDetailBean> currentData = new ArrayList<PesananDetailBean>();
		Cursor dataCursor = database.query(helper.TABLE_PESANAN_DETAIL, 
				new String[] {helper.ID_PESANAN_DETAIL, helper.ID_BARANG1, helper.ID_PESANAN1, 
				helper.KUANTITAS_PESANAN, helper.TOTAL, helper.POTONGAN}, 
				null, null, null, null, null);
		
		dataCursor.moveToFirst(); PesananDetailBean t;		
		if ( !dataCursor.isAfterLast() ) {
			do {
				int idPesananDetail = dataCursor.getInt(0);
				String idBarang = dataCursor.getString(1);
				String idPesanan = dataCursor.getString(2); 
				Integer kuantitasPesanan = dataCursor.getInt(3);
				Float total = dataCursor.getFloat(4);
				Float potongan = dataCursor.getFloat(5);
				
				
								
				t = new PesananDetailBean(); 
				t.setIDPesananDetail(""+idPesananDetail);
				t.setIDBarang(idBarang);
				t.setIDPesanan(idPesanan); 
				t.setKuantitasPesanan(kuantitasPesanan);
				t.setTotal(total);
				t.setPotongan(potongan);
				currentData.add(t);
				
			} while(dataCursor.moveToNext());
		}		
		dataCursor.close();		
		return currentData;
	}

	/*@SuppressWarnings("static-access")
	public void onSaveData(PesananDetailBean getPesanan) {
    	assert (null != getPesanan);
    	
    	ContentValues values = new ContentValues();
		values.put(helper.ID_BARANG1, getPesanan.getIDBarang());
		values.put(helper.ID_PESANAN1, getPesanan.getIDPesanan());
		values.put(helper.KUANTITAS_PESANAN, getPesanan.getKuantitasPesanan());
		
//		getPesanan.setIDPesanan(database.insert(helper.TABLE_PESANAN_DETAIL, null, values));
    }*/
	@SuppressWarnings("static-access")
	public void onSaveData(ArrayList<PesananDetailBean> itemAll) {
		// TODO Auto-generated method stub
		for (int i = 0; i < itemAll.size(); i++) {
			ContentValues values = new ContentValues();
			values.put(helper.ID_BARANG1, itemAll.get(i).getIDBarang());
			values.put(helper.ID_PESANAN1, itemAll.get(i).getIDPesanan());
			values.put(helper.KUANTITAS_PESANAN, itemAll.get(i).getKuantitasPesanan());
			values.put(helper.TOTAL, itemAll.get(i).getTotal());
			values.put(helper.POTONGAN, itemAll.get(i).getPotongan());
			Log.d(TAG, itemAll.get(i).getIDBarang());
			Log.d(TAG, itemAll.get(i).getIDPesanan());
			Log.d(TAG, ""+itemAll.get(i).getKuantitasPesanan());
			Log.d(TAG, ""+itemAll.get(i).getTotal());
			Log.d(TAG, ""+itemAll.get(i).getPotongan());
			itemAll.get(i).setIDPesananDetail(""+database.insert(helper.TABLE_PESANAN_DETAIL, null, values));
    	}
	}
	
	
	@SuppressWarnings("static-access")
	public void onUpdateData(PesananDetailBean getPesanan) {
    	assert (null != getPesanan);    	
    	ContentValues values = new ContentValues();
		values.put(helper.KUANTITAS_PESANAN, getPesanan.getKuantitasPesanan());
		values.put(helper.TOTAL, getPesanan.getTotal());
		values.put(helper.POTONGAN, getPesanan.getPotongan());
				
		String idPesanan = getPesanan.getIDPesanan();
//		String idBarang = getPesanan.getIDBarang();
		String where = String.format("%s = %d", helper.ID_PESANAN1, idPesanan );
//		String.format("%s = %d", helper.ID_BARANG1, idBarang);
		database.update(helper.TABLE_PESANAN, values, where, null); 
    }
}
